<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>S110_nRF51822/Source/ble/ble_bondmngr.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css" />
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="alias.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="alias.js"></script>

<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>


<script type="text/javascript">
  jQuery(document).ready(function () {
    if(gref){ // Number all _img and _table classes
      gref();
    }
  });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->

<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">nRF51822 BLE SDK
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('ble__bondmngr_8c.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">S110_nRF51822/Source/ble/ble_bondmngr.c</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.</span>
<a name="l00002"></a>00002 <span class="comment"> *</span>
<a name="l00003"></a>00003 <span class="comment"> * The information contained herein is property of Nordic Semiconductor ASA.</span>
<a name="l00004"></a>00004 <span class="comment"> * Terms and conditions of usage are described in detail in NORDIC</span>
<a name="l00005"></a>00005 <span class="comment"> * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Licensees are granted free, non-transferable use of the information. NO</span>
<a name="l00008"></a>00008 <span class="comment"> * WARRANTY of ANY KIND is provided. This heading must NOT be removed from</span>
<a name="l00009"></a>00009 <span class="comment"> * the file.</span>
<a name="l00010"></a>00010 <span class="comment"> *</span>
<a name="l00011"></a>00011 <span class="comment"> * $LastChangedRevision$</span>
<a name="l00012"></a>00012 <span class="comment"> */</span>
<a name="l00013"></a>00013 
<a name="l00014"></a>00014 <span class="preprocessor">#include &quot;<a class="code" href="ble__bondmngr_8h.html">ble_bondmngr.h</a>&quot;</span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &lt;stdint.h&gt;</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;<a class="code" href="nordic__common_8h.html" title="Common defines and macros for firmware developed by Nordic Semiconductor.">nordic_common.h</a>&quot;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &quot;nrf_error.h&quot;</span>
<a name="l00020"></a>00020 <span class="preprocessor">#include &quot;ble_gap.h&quot;</span>
<a name="l00021"></a>00021 <span class="preprocessor">#include &quot;<a class="code" href="ble__srv__common_8h.html">ble_srv_common.h</a>&quot;</span>
<a name="l00022"></a>00022 <span class="preprocessor">#include &quot;<a class="code" href="ble__sdk__util_8h.html">ble_sdk_util.h</a>&quot;</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include &quot;<a class="code" href="nrf__assert_8h.html" title="Utilities for verifying program logic.">nrf_assert.h</a>&quot;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &quot;nrf.h&quot;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include &quot;nrf51_bitfields.h&quot;</span>
<a name="l00026"></a>00026 <span class="preprocessor">#include &quot;<a class="code" href="ble__flash_8h.html">ble_flash.h</a>&quot;</span>
<a name="l00027"></a>00027 <span class="preprocessor">#include &quot;ble_bondmngr_cfg.h&quot;</span>
<a name="l00028"></a>00028 
<a name="l00029"></a>00029 
<a name="l00030"></a>00030 <span class="preprocessor">#define FLASH_PAGE                  255                                                 </span>
<a name="l00031"></a>00031 <span class="preprocessor">#define INVALID_MASTER_HANDLE       -1                                                  </span>
<a name="l00032"></a>00032 <span class="preprocessor">#define CCCD_SIZE                   6                                                   </span>
<a name="l00033"></a>00033 <span class="preprocessor">#define CRC_SIZE                    2                                                   </span>
<a name="l00034"></a>00034 <span class="preprocessor">#define SYS_ATTR_BUFFER_MAX_LEN     ((BLE_BONDMNGR_CCCD_COUNT * CCCD_SIZE) + CRC_SIZE)  </span>
<a name="l00036"></a>00036 <span class="preprocessor">#define BONDED_MASTERS_DB_SIZE \</span>
<a name="l00037"></a>00037 <span class="preprocessor">    CEIL_DIV(sizeof(master_data_t) * BLE_BONDMNGR_MAX_BONDED_MASTERS, sizeof(uint32_t)) </span>
<a name="l00041"></a>00041 <span class="preprocessor">typedef struct</span>
<a name="l00042"></a>00042 <span class="preprocessor"></span>{
<a name="l00043"></a>00043     <a class="code" href="structble__gap__evt__auth__status__t.html" title="Event data for authentication status event.">ble_gap_evt_auth_status_t</a>       auth_status;                                        
<a name="l00044"></a>00044     uint8_t                         sys_attr[SYS_ATTR_BUFFER_MAX_LEN];                  
<a name="l00045"></a>00045     uint16_t                        sys_attr_size;                                      
<a name="l00046"></a>00046     <a class="code" href="structble__gap__evt__sec__info__request__t.html" title="Event data for securito info request event.">ble_gap_evt_sec_info_request_t</a>  master_id_info;                                     
<a name="l00047"></a>00047     int8_t                          master_handle;                                      
<a name="l00048"></a>00048 } master_data_t;
<a name="l00049"></a>00049 
<a name="l00050"></a>00050 <span class="keyword">static</span> uint16_t                     <a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>;                                      
<a name="l00051"></a>00051 <span class="keyword">static</span> master_data_t                s_master;                                           
<a name="l00052"></a>00052 <span class="keyword">static</span> uint32_t                     s_bonded_masters_db[BONDED_MASTERS_DB_SIZE];        
<a name="l00053"></a>00053 <span class="keyword">static</span> uint8_t                      s_bonded_masters_count;                             
<a name="l00054"></a>00054 <span class="keyword">static</span> master_data_t *              p_bonded_masters_db;                                
<a name="l00057"></a>00057 <span class="keyword">static</span> uint32_t master_add(<span class="keywordtype">void</span>)
<a name="l00058"></a>00058 {
<a name="l00059"></a>00059     uint32_t err_code;
<a name="l00060"></a>00060     
<a name="l00061"></a>00061     <span class="keywordflow">if</span> (s_master.master_handle == INVALID_MASTER_HANDLE)
<a name="l00062"></a>00062     {
<a name="l00063"></a>00063         <span class="keywordflow">if</span> (s_bonded_masters_count &lt; BLE_BONDMNGR_MAX_BONDED_MASTERS)
<a name="l00064"></a>00064         {
<a name="l00065"></a>00065             s_master.master_handle = s_bonded_masters_count;
<a name="l00066"></a>00066             p_bonded_masters_db[s_bonded_masters_count] = s_master;
<a name="l00067"></a>00067             s_bonded_masters_count++;
<a name="l00068"></a>00068             err_code = <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00069"></a>00069         }
<a name="l00070"></a>00070         <span class="keywordflow">else</span>
<a name="l00071"></a>00071         {
<a name="l00072"></a>00072             err_code = <a class="code" href="group__nrf__error.html#ga7ef4d466b79b31c6325f8b7af5d4f75f" title="No Memory for operation.">NRF_ERROR_NO_MEM</a>;
<a name="l00073"></a>00073         }
<a name="l00074"></a>00074     }
<a name="l00075"></a>00075     <span class="keywordflow">else</span>
<a name="l00076"></a>00076     {
<a name="l00077"></a>00077         <span class="keywordflow">if</span> ((s_master.master_handle &gt;= 0) &amp;&amp; (s_master.master_handle &lt; s_bonded_masters_count))
<a name="l00078"></a>00078         {
<a name="l00079"></a>00079             p_bonded_masters_db[s_master.master_handle] = s_master;
<a name="l00080"></a>00080             err_code = <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00081"></a>00081         }
<a name="l00082"></a>00082         <span class="keywordflow">else</span>
<a name="l00083"></a>00083         {
<a name="l00084"></a>00084             err_code = <a class="code" href="group__nrf__error.html#ga0a5831cf5092e0dd43a01869676ee076" title="Invalid Parameter.">NRF_ERROR_INVALID_PARAM</a>;
<a name="l00085"></a>00085         }
<a name="l00086"></a>00086     }
<a name="l00087"></a>00087     
<a name="l00088"></a>00088     <span class="keywordflow">return</span> err_code;
<a name="l00089"></a>00089 }
<a name="l00090"></a>00090 
<a name="l00091"></a>00091 
<a name="l00092"></a>00092 <span class="keyword">static</span> uint32_t master_find(<span class="keyword">const</span> uint16_t master_div)
<a name="l00093"></a>00093 {
<a name="l00094"></a>00094     <span class="keywordtype">int</span> i;
<a name="l00095"></a>00095     
<a name="l00096"></a>00096     s_master.master_handle = INVALID_MASTER_HANDLE;
<a name="l00097"></a>00097     <span class="keywordflow">for</span> (i = 0; i &lt; s_bonded_masters_count; i++)
<a name="l00098"></a>00098     {
<a name="l00099"></a>00099         <span class="keywordflow">if</span> (master_div == p_bonded_masters_db[i].master_id_info.div)
<a name="l00100"></a>00100         {
<a name="l00101"></a>00101             s_master = p_bonded_masters_db[i];
<a name="l00102"></a>00102             <span class="keywordflow">return</span> <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00103"></a>00103         }
<a name="l00104"></a>00104     }
<a name="l00105"></a>00105     
<a name="l00106"></a>00106     <span class="keywordflow">return</span> <a class="code" href="group__nrf__error.html#ga349d25ada15be023e0d507f45ada682c" title="Not found.">NRF_ERROR_NOT_FOUND</a>;
<a name="l00107"></a>00107 }
<a name="l00108"></a>00108 
<a name="l00109"></a>00109 
<a name="l00110"></a><a class="code" href="group__ble__sdk__lib__bond__manager.html#ga8e2aacd9f59e03d60b8db1ee0819c296">00110</a> <span class="keywordtype">void</span> <a class="code" href="group__ble__sdk__lib__bond__manager.html#ga8e2aacd9f59e03d60b8db1ee0819c296" title="Delete the bonded master database from flash.">ble_bondmngr_master_delete</a>(<span class="keywordtype">void</span>)
<a name="l00111"></a>00111 {
<a name="l00112"></a>00112     <a class="code" href="group__ble__flash__module.html#gae80848c09ea4534c232ecc19fe5c1221" title="Initializes the flash manager.">ble_flash_init</a>(<span class="keyword">sizeof</span>(master_data_t));
<a name="l00113"></a>00113     <a class="code" href="group__ble__flash__module.html#gac63ba6cc6f4b138d9a7d6ac9d734b30b" title="Erases a flash page.">ble_flash_page_erase</a>(FLASH_PAGE);
<a name="l00114"></a>00114 }
<a name="l00115"></a>00115 
<a name="l00116"></a>00116 
<a name="l00117"></a>00117 <span class="keyword">static</span> uint32_t on_connect(<a class="code" href="structble__evt__t.html" title="Common BLE Event type, wrapping the module specific event reports.">ble_evt_t</a> * p_ble_evt)
<a name="l00118"></a>00118 {
<a name="l00119"></a>00119     <a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a> = p_ble_evt-&gt;evt.<a class="code" href="structble__evt__t.html#a91857ca158f0c4e43f610068772c7d3d">gatts_evt</a>.<a class="code" href="structble__gatts__evt__t.html#a0d5ffe38d68e48d81e61fc6a4999ae68">conn_handle</a>;
<a name="l00120"></a>00120     <span class="keywordflow">return</span> <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00121"></a>00121 }
<a name="l00122"></a>00122 
<a name="l00123"></a>00123 
<a name="l00124"></a>00124 <span class="keyword">static</span> uint32_t on_disconnect(<a class="code" href="structble__evt__t.html" title="Common BLE Event type, wrapping the module specific event reports.">ble_evt_t</a> * p_ble_evt)
<a name="l00125"></a>00125 {
<a name="l00126"></a>00126     uint32_t err_code;
<a name="l00127"></a>00127 
<a name="l00128"></a>00128     err_code = <a class="code" href="group___b_l_e___g_a_t_t_s.html#gaef948f6aafa9e8a728a948bec76e8208" title="Retrieve persistent system attribute info from softdevice.">ble_gatts_sys_attr_get</a>(<a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>, s_master.sys_attr, &amp;s_master.sys_attr_size); 
<a name="l00129"></a>00129     <span class="keywordflow">if</span> (err_code != <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>)
<a name="l00130"></a>00130     {
<a name="l00131"></a>00131         <span class="keywordflow">return</span> err_code;
<a name="l00132"></a>00132     }
<a name="l00133"></a>00133     
<a name="l00134"></a>00134     err_code               = master_add();
<a name="l00135"></a>00135     <a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>          = <a class="code" href="group___b_l_e___c_o_n_n___h_a_n_d_l_e_s.html#gacf227b9b101dbcf08eccbbaba54e48f5">BLE_CONN_HANDLE_INVALID</a>;
<a name="l00136"></a>00136     s_master.master_handle = INVALID_MASTER_HANDLE;
<a name="l00137"></a>00137 
<a name="l00138"></a>00138     <span class="keywordflow">return</span> err_code;
<a name="l00139"></a>00139 }
<a name="l00140"></a>00140 
<a name="l00141"></a>00141 
<a name="l00142"></a>00142 <span class="keyword">static</span> uint32_t on_sys_attr_missing(<a class="code" href="structble__evt__t.html" title="Common BLE Event type, wrapping the module specific event reports.">ble_evt_t</a> * p_ble_evt)
<a name="l00143"></a>00143 {
<a name="l00144"></a>00144     <span class="keywordflow">if</span> (s_master.master_handle == INVALID_MASTER_HANDLE)
<a name="l00145"></a>00145     {
<a name="l00146"></a>00146         <span class="keywordflow">return</span> <a class="code" href="group___b_l_e___g_a_t_t_s.html#ga65ae169e49530eb2fa4bf73053060a13" title="Update persistent system attribute info.">ble_gatts_sys_attr_set</a>(<a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>, NULL, 0);
<a name="l00147"></a>00147     }
<a name="l00148"></a>00148     <span class="keywordflow">else</span>
<a name="l00149"></a>00149     {
<a name="l00150"></a>00150         <span class="comment">// Current master is valid, use its data. Set the corresponding sys_attr.</span>
<a name="l00151"></a>00151         <span class="keywordflow">return</span> <a class="code" href="group___b_l_e___g_a_t_t_s.html#ga65ae169e49530eb2fa4bf73053060a13" title="Update persistent system attribute info.">ble_gatts_sys_attr_set</a>(<a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>, s_master.sys_attr, s_master.sys_attr_size);
<a name="l00152"></a>00152     }
<a name="l00153"></a>00153 }
<a name="l00154"></a>00154 
<a name="l00155"></a>00155 
<a name="l00156"></a>00156 <span class="keyword">static</span> uint32_t on_auth_status(<a class="code" href="structble__gap__evt__auth__status__t.html" title="Event data for authentication status event.">ble_gap_evt_auth_status_t</a> * p_auth_status)
<a name="l00157"></a>00157 {
<a name="l00158"></a>00158     uint32_t err_code = <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00159"></a>00159 
<a name="l00160"></a>00160     <span class="keywordflow">if</span> (s_master.master_handle == INVALID_MASTER_HANDLE)
<a name="l00161"></a>00161     {
<a name="l00162"></a>00162         err_code = master_find(p_auth_status-&gt;periph_keys.<a class="code" href="structble__gap__evt__auth__status__t_1_1periph__keys__t.html#a7eee7ec3b96cc86a0080ab368dae817d">enc_info</a>.<a class="code" href="structble__gap__enc__info__t.html#aa1147a1ab6ff4c36934461ca71d73bef">div</a>);
<a name="l00163"></a>00163         <span class="keywordflow">if</span> (err_code == <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>)
<a name="l00164"></a>00164         {
<a name="l00165"></a>00165             <span class="comment">// Master found in the list of bonded master.</span>
<a name="l00166"></a>00166             <span class="comment">// Set the corresponding sys_attr.</span>
<a name="l00167"></a>00167             err_code = <a class="code" href="group___b_l_e___g_a_t_t_s.html#ga65ae169e49530eb2fa4bf73053060a13" title="Update persistent system attribute info.">ble_gatts_sys_attr_set</a>(<a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>, 
<a name="l00168"></a>00168                                               s_master.sys_attr, 
<a name="l00169"></a>00169                                               s_master.sys_attr_size);
<a name="l00170"></a>00170         }
<a name="l00171"></a>00171         <span class="keywordflow">if</span> (err_code == <a class="code" href="group__nrf__error.html#ga349d25ada15be023e0d507f45ada682c" title="Not found.">NRF_ERROR_NOT_FOUND</a>)
<a name="l00172"></a>00172         {
<a name="l00173"></a>00173             err_code = <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00174"></a>00174         }
<a name="l00175"></a>00175     }
<a name="l00176"></a>00176     
<a name="l00177"></a>00177     s_master.auth_status        = *p_auth_status;
<a name="l00178"></a>00178     s_master.master_id_info.div = p_auth_status-&gt;periph_keys.<a class="code" href="structble__gap__evt__auth__status__t_1_1periph__keys__t.html#a7eee7ec3b96cc86a0080ab368dae817d">enc_info</a>.<a class="code" href="structble__gap__enc__info__t.html#aa1147a1ab6ff4c36934461ca71d73bef">div</a>;
<a name="l00179"></a>00179     
<a name="l00180"></a>00180     <span class="keywordflow">return</span> err_code;
<a name="l00181"></a>00181 }
<a name="l00182"></a>00182 
<a name="l00183"></a>00183 
<a name="l00184"></a>00184 <span class="keyword">static</span> uint32_t on_sec_info_request(<a class="code" href="structble__evt__t.html" title="Common BLE Event type, wrapping the module specific event reports.">ble_evt_t</a> * p_ble_evt)
<a name="l00185"></a>00185 {
<a name="l00186"></a>00186     uint32_t err_code;
<a name="l00187"></a>00187 
<a name="l00188"></a>00188     s_master.master_id_info = p_ble_evt-&gt;evt.<a class="code" href="structble__evt__t.html#a2fd98f8868b9aea30d35bf71d41d646a">gap_evt</a>.params.<a class="code" href="structble__gap__evt__t.html#a5f04f763d053c94297246b6ed351c66a">sec_info_request</a>;
<a name="l00189"></a>00189     
<a name="l00190"></a>00190     err_code = master_find(s_master.master_id_info.div);
<a name="l00191"></a>00191     <span class="keywordflow">if</span> (err_code == <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>)
<a name="l00192"></a>00192     {
<a name="l00193"></a>00193         <span class="comment">// Master found in the list of bonded master. Use the encryption info for this master.</span>
<a name="l00194"></a>00194         err_code = <a class="code" href="group___b_l_e___g_a_p.html#ga31de499e23c1d7876612af2fa65f3c46" title="Reply with GAP security information.">ble_gap_sec_info_reply</a>(<a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>, 
<a name="l00195"></a>00195                                           &amp;s_master.auth_status.periph_keys.enc_info, 
<a name="l00196"></a>00196                                           NULL);
<a name="l00197"></a>00197         <span class="keywordflow">if</span> (err_code != <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>)
<a name="l00198"></a>00198         {
<a name="l00199"></a>00199             <span class="keywordflow">return</span> err_code;
<a name="l00200"></a>00200         }
<a name="l00201"></a>00201         
<a name="l00202"></a>00202         <span class="comment">// In addition set the corresponding sys_attr</span>
<a name="l00203"></a>00203         err_code = <a class="code" href="group___b_l_e___g_a_t_t_s.html#ga65ae169e49530eb2fa4bf73053060a13" title="Update persistent system attribute info.">ble_gatts_sys_attr_set</a>(<a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>, s_master.sys_attr, s_master.sys_attr_size);
<a name="l00204"></a>00204     }
<a name="l00205"></a>00205     <span class="keywordflow">else</span> <span class="keywordflow">if</span> (err_code == <a class="code" href="group__nrf__error.html#ga349d25ada15be023e0d507f45ada682c" title="Not found.">NRF_ERROR_NOT_FOUND</a>)
<a name="l00206"></a>00206     {
<a name="l00207"></a>00207         <span class="comment">// New master</span>
<a name="l00208"></a>00208         err_code = <a class="code" href="group___b_l_e___g_a_p.html#ga31de499e23c1d7876612af2fa65f3c46" title="Reply with GAP security information.">ble_gap_sec_info_reply</a>(<a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>, NULL, NULL);
<a name="l00209"></a>00209         <span class="keywordflow">if</span> (err_code != <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>)
<a name="l00210"></a>00210         {
<a name="l00211"></a>00211             <span class="keywordflow">return</span> err_code;
<a name="l00212"></a>00212         }
<a name="l00213"></a>00213         
<a name="l00214"></a>00214         <span class="comment">// Initialize the sys_attr</span>
<a name="l00215"></a>00215         err_code = <a class="code" href="group___b_l_e___g_a_t_t_s.html#ga65ae169e49530eb2fa4bf73053060a13" title="Update persistent system attribute info.">ble_gatts_sys_attr_set</a>(<a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>, NULL, 0);
<a name="l00216"></a>00216     }
<a name="l00217"></a>00217     
<a name="l00218"></a>00218     <span class="keywordflow">return</span> err_code;
<a name="l00219"></a>00219 }
<a name="l00220"></a>00220 
<a name="l00221"></a>00221 
<a name="l00222"></a><a class="code" href="group__ble__sdk__lib__bond__manager.html#ga2f338b201c43be781a0194a97a243318">00222</a> uint32_t <a class="code" href="group__ble__sdk__lib__bond__manager.html#ga2f338b201c43be781a0194a97a243318" title="Function to be called before going to system off, in order to save all bonded masters in flash memory...">ble_bondmngr_on_goto_system_off</a>(<span class="keywordtype">void</span>)
<a name="l00223"></a>00223 {
<a name="l00224"></a>00224     uint32_t err_code = <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00225"></a>00225 
<a name="l00226"></a>00226     <span class="keywordflow">if</span> (s_bonded_masters_count != 0)
<a name="l00227"></a>00227     {
<a name="l00228"></a>00228         err_code = <a class="code" href="group__ble__flash__module.html#ga4aef9cc7c514d95befafb92d35d8a4a0" title="Writes the flash at the given page with the given data.">ble_flash_write</a>(FLASH_PAGE, s_bonded_masters_db, s_bonded_masters_count);
<a name="l00229"></a>00229     }
<a name="l00230"></a>00230     
<a name="l00231"></a>00231     <span class="keywordflow">return</span> err_code;
<a name="l00232"></a>00232 }
<a name="l00233"></a>00233 
<a name="l00234"></a>00234 
<a name="l00235"></a><a class="code" href="group__ble__sdk__lib__bond__manager.html#ga3996361c2235fe463273d331b8ea8678">00235</a> uint32_t <a class="code" href="group__ble__sdk__lib__bond__manager.html#ga3996361c2235fe463273d331b8ea8678" title="Initializes the Bond Manager.">ble_bondmngr_init</a>(<span class="keywordtype">void</span>)
<a name="l00236"></a>00236 {
<a name="l00237"></a>00237     <span class="keywordtype">int</span>      i;
<a name="l00238"></a>00238     uint32_t err_code;
<a name="l00239"></a>00239    
<a name="l00240"></a>00240     memset(&amp;s_master, 0, <span class="keyword">sizeof</span>(master_data_t));
<a name="l00241"></a>00241     
<a name="l00242"></a>00242     s_master.sys_attr_size = SYS_ATTR_BUFFER_MAX_LEN;
<a name="l00243"></a>00243     s_master.master_handle = INVALID_MASTER_HANDLE;
<a name="l00244"></a>00244     <a class="code" href="group__ble__sdk__app__hrs__main.html#gaaf76d22968f9f320aa5005a25f654320">s_conn_handle</a>          = <a class="code" href="group___b_l_e___c_o_n_n___h_a_n_d_l_e_s.html#gacf227b9b101dbcf08eccbbaba54e48f5">BLE_CONN_HANDLE_INVALID</a>;
<a name="l00245"></a>00245     p_bonded_masters_db    = (master_data_t *)(s_bonded_masters_db);
<a name="l00246"></a>00246 
<a name="l00247"></a>00247     <a class="code" href="group__ble__flash__module.html#gae80848c09ea4534c232ecc19fe5c1221" title="Initializes the flash manager.">ble_flash_init</a>(<span class="keyword">sizeof</span>(master_data_t));
<a name="l00248"></a>00248     
<a name="l00249"></a>00249     err_code = <a class="code" href="group__ble__flash__module.html#ga6602056a8dbec6f010b5f717fed92bf8" title="Reads data from flash to RAM.">ble_flash_read</a>(FLASH_PAGE, s_bonded_masters_db, &amp;s_bonded_masters_count);
<a name="l00250"></a>00250     <span class="keywordflow">if</span> (err_code == <a class="code" href="group__nrf__error.html#ga349d25ada15be023e0d507f45ada682c" title="Not found.">NRF_ERROR_NOT_FOUND</a>)
<a name="l00251"></a>00251     {
<a name="l00252"></a>00252         <span class="comment">// The flash does not contain any memorized masters, initialize the RAM DB with default.</span>
<a name="l00253"></a>00253         s_bonded_masters_count = 0;
<a name="l00254"></a>00254         <span class="keywordflow">for</span> (i = 0; i &lt; BLE_BONDMNGR_MAX_BONDED_MASTERS; i++)
<a name="l00255"></a>00255         {
<a name="l00256"></a>00256             p_bonded_masters_db[i].master_handle = INVALID_MASTER_HANDLE;
<a name="l00257"></a>00257         }
<a name="l00258"></a>00258         err_code = <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00259"></a>00259     }
<a name="l00260"></a>00260     
<a name="l00261"></a>00261     <span class="keywordflow">return</span> err_code;
<a name="l00262"></a>00262 }
<a name="l00263"></a>00263 
<a name="l00264"></a>00264 
<a name="l00265"></a><a class="code" href="group__ble__sdk__lib__bond__manager.html#ga0602a76664be6f6f19997368d9cf8860">00265</a> uint32_t <a class="code" href="group__ble__sdk__lib__bond__manager.html#ga0602a76664be6f6f19997368d9cf8860" title="Handles all events from the BLE stack that are of interest to this module.">ble_bondmngr_on_ble_evt</a>(<a class="code" href="structble__evt__t.html" title="Common BLE Event type, wrapping the module specific event reports.">ble_evt_t</a> * p_ble_evt)
<a name="l00266"></a>00266 {
<a name="l00267"></a>00267     uint32_t err_code = <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00268"></a>00268     
<a name="l00269"></a>00269     <span class="keywordflow">switch</span> (p_ble_evt-&gt;<a class="code" href="structble__evt__t.html#a50f33abba5627a26728ba30fffd9880a">header</a>.<a class="code" href="structble__evt__hdr__t.html#a826314e7ea25d4267621899efe6859f9">evt_id</a>)
<a name="l00270"></a>00270     {
<a name="l00271"></a>00271         <span class="keywordflow">case</span> <a class="code" href="group___b_l_e___g_a_p.html#ggabc5c98fcc1211af2b80116dd6e0a035daa0b4789724d202a13a5e7eab85c52957">BLE_GAP_EVT_CONNECTED</a>:
<a name="l00272"></a>00272             err_code = on_connect(p_ble_evt);
<a name="l00273"></a>00273             <span class="keywordflow">break</span>;
<a name="l00274"></a>00274             
<a name="l00275"></a>00275         <span class="keywordflow">case</span> <a class="code" href="group___b_l_e___g_a_p.html#ggabc5c98fcc1211af2b80116dd6e0a035da057001bbd6ea9e615f19ef93cc0831ee">BLE_GAP_EVT_DISCONNECTED</a>:
<a name="l00276"></a>00276             err_code = on_disconnect(p_ble_evt);
<a name="l00277"></a>00277             <span class="keywordflow">break</span>;
<a name="l00278"></a>00278             
<a name="l00279"></a>00279         <span class="keywordflow">case</span> <a class="code" href="group___b_l_e___g_a_t_t_s.html#gga80155586fa275b28773c9b203f52cabaae3776204f11a583e105aed0e477601da">BLE_GATTS_EVT_SYS_ATTR_MISSING</a>:
<a name="l00280"></a>00280             err_code = on_sys_attr_missing(p_ble_evt);
<a name="l00281"></a>00281             <span class="keywordflow">break</span>;
<a name="l00282"></a>00282 
<a name="l00283"></a>00283         <span class="keywordflow">case</span> <a class="code" href="group___b_l_e___g_a_p.html#ggabc5c98fcc1211af2b80116dd6e0a035da8958220152f7547da3b250b4f724877c">BLE_GAP_EVT_AUTH_STATUS</a>:
<a name="l00284"></a>00284             err_code = on_auth_status(&amp;p_ble_evt-&gt;evt.<a class="code" href="structble__evt__t.html#a2fd98f8868b9aea30d35bf71d41d646a">gap_evt</a>.params.<a class="code" href="structble__gap__evt__t.html#a350f0c3a127499fb3d522d361554e3eb">auth_status</a>);
<a name="l00285"></a>00285             <span class="keywordflow">break</span>;
<a name="l00286"></a>00286             
<a name="l00287"></a>00287         <span class="keywordflow">case</span> <a class="code" href="group___b_l_e___g_a_p.html#ggabc5c98fcc1211af2b80116dd6e0a035daf1c60079f92820cb933ebdb66d2d8d7d">BLE_GAP_EVT_SEC_INFO_REQUEST</a>:
<a name="l00288"></a>00288             err_code = on_sec_info_request(p_ble_evt);
<a name="l00289"></a>00289             <span class="keywordflow">break</span>;
<a name="l00290"></a>00290             
<a name="l00291"></a>00291         <span class="keywordflow">default</span>:
<a name="l00292"></a>00292             <span class="keywordflow">break</span>;
<a name="l00293"></a>00293     }
<a name="l00294"></a>00294     
<a name="l00295"></a>00295     <span class="keywordflow">return</span> err_code;
<a name="l00296"></a>00296 }
</pre></div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><b>ble_bondmngr.c</b>      </li>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>


    <li class="footer">
      Copyright &copy 2006-2011 <a href="http://www.nordicsemi.no" style="text-decoration:none">Nordic Semiconductor</a>.
      All Rights Reserved.
      <a href="disclaimer.html">Disclaimer</a>
    </li>
   </ul>
 </div>


</body>
</html>
